﻿using PwjCy.Lite.Core;
using PwjCy.Lite.Security;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace PwjCy.Lite.Storage.Imp
{
    /// <summary>
    /// 数据库连接
    /// </summary>
    public abstract class AbstractConnection : IConnection
    {
        protected IDbConnection _conn;
        protected IStorageContext _storageContext;
        protected IPasswordGenerateService _PasswordGenerateService;

        protected AbstractConnection(IStorageContext storageContext, IDbConnection conn)
        {
            this._storageContext = storageContext;
            this._conn = conn;
            _PasswordGenerateService = (IPasswordGenerateService)ServiceHub.Instance.GetService(typeof(IPasswordGenerateService));
        }
        public virtual IDbTransaction BeginTransaction() { return _conn.BeginTransaction(); }
        public virtual IDbTransaction BeginTransaction(IsolationLevel iso) { return _conn.BeginTransaction(iso); }
        public void Close()
        {
            _conn.Close();
        }
        public abstract IStatement CreateStatement(string sql);
        public bool ExistTable(string tableName)
        {
            throw new NotImplementedException();
        }
        public abstract DataTable GetOleDbSchemaTable(Guid schema, object[] restrictions);
        public abstract string GetParameterPlaceHolder(string key);
        public abstract DataTable GetSchemaTables(string catalog, string schema, string name, string type);
        public void Open()
        {
            _conn.Open();
        }
        public abstract ICallableStatement PrpareCall(string sql);
        public abstract IPreparedStatement PrepareStatement(string sql);
        public void ReOpen()
        {
            _conn.Close();
            _conn.Open();
        }

        public object InnerConnection
        {
            get { return _conn; }
        }

        public ConnectionState State
        {
            get
            {
                return _conn.State;
            }
        }

        public IStorageContext StorageContext
        {
            get { return _storageContext; }
        }
    }
}
